//
//  PageTitleView.swift
//  DouYuZB
//
//  Created by James on 2017/10/14.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

// MARK: - 协议
protocol PageTitleViewDelegate:class {//这样写表示只能被类遵守
    func pageTitleView(titleView:PageTitleView, selectIndex index: Int)
}

private let kNormalColor : (CGFloat,CGFloat,CGFloat) = (85,85,85)
private let kSelectColor : (CGFloat,CGFloat,CGFloat) = (255,128,0)

private let kScrollLineH : CGFloat = 2

class PageTitleView: UIView {
    // MARK: -定义属性
    fileprivate var titles:[String]?
    fileprivate var currentIndex: Int = 0
    fileprivate lazy var scrollView:UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.frame = self.bounds
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        }
        return scrollView
    }()
    
    lazy var scrollLine : UIView = {
       let view = UIView()
        view.backgroundColor = UIColor.orange
        return view
    }()
    
    lazy var titleLabels:[UILabel] = [UILabel]()
    
    //代理
    weak var delegate: PageTitleViewDelegate?
    
    // MARK: -自定义构造函数
    init(frame: CGRect,titles:[String]) {
        self.titles = titles
        super.init(frame: frame)
        //设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

extension PageTitleView{
    private func setupUI()  {
        //0.设置底线
        setupBottomMenuLine()
        //1.加scrollview
        self.addSubview(scrollView)
        //2.加label
        setupLabels()
        //3.滚动滑块
        setupScrollLine()
    }
    
    private func setupBottomMenuLine() {
        let bottomLine = UIView()
        bottomLine.frame = CGRect(x: 0, y: frame.height-kScrollLineH, width: frame.width, height: kScrollLineH)
        bottomLine.backgroundColor = UIColor.lightGray
        self.addSubview(bottomLine)
    }
    
    private func setupScrollLine()  {
        guard let label = titleLabels.first else { return }
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: label.frame.origin.x, y: frame.height - kScrollLineH, width: label.frame.width, height: kScrollLineH)
        
        label.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
    }
    
    private func setupLabels() {
        
        let w:CGFloat = frame.width / CGFloat(titles!.count)
        let h:CGFloat = frame.height - kScrollLineH
        let y:CGFloat = 0
        
        for (index,title) in titles!.enumerated() {
            let label = UILabel()
            
            label.text = title
            label.tag = index
            label.font = UIFont.systemFont(ofSize: 16)
            label.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            label.textAlignment = .center
            
            let x:CGFloat = w * CGFloat(index)
    
            label.frame = CGRect(x: x, y: y, width: w, height: h)
            titleLabels.append(label)
            scrollView.addSubview(label)
            
            //添加点击手势
            label.isUserInteractionEnabled = true
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(tapAction(tapGes:)))
            label.addGestureRecognizer(tapGes)
        }
    }
}

extension PageTitleView {
    @objc func tapAction(tapGes:UITapGestureRecognizer)  {
        if tapGes.view?.tag == currentIndex { return }
        //获取点击的label
        guard let label = tapGes.view as? UILabel else { return }
        
        label.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        
        let oldLabel = titleLabels[currentIndex]
        oldLabel.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        
        currentIndex = label.tag
        //做动画
        let scrollLinePosion:CGFloat = CGFloat(currentIndex) * scrollLine.frame.width
        UIView.animate(withDuration: 0.3) {
            self.scrollLine.frame.origin.x = scrollLinePosion
        }
        //传值给代理
        delegate?.pageTitleView(titleView: self, selectIndex: currentIndex)
    }
    
    func setTitleWithProgress(progress:CGFloat,sourceIndex:Int,targetIndex:Int){
        //取出label
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        //处理滑块
        let moveTotalX = targetLabel.frame.origin.x -  sourceLabel.frame.origin.x
        let moveX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        
        //title 颜色渐变
        //1.取出颜色渐变的范围
        let colorRange = (kSelectColor.0 - kNormalColor.0,kSelectColor.1 - kNormalColor.1,kSelectColor.2 - kNormalColor.2)
        
        //2.变化label
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - colorRange.0 * progress, g: kSelectColor.1 - colorRange.1 * progress, b: kSelectColor.2 - colorRange.2 * progress)
        targetLabel.textColor = UIColor(r: kNormalColor.0 + colorRange.0 * progress, g: kNormalColor.1 + colorRange.1 * progress, b: kNormalColor.2 + colorRange.2 * progress)
        
        //3.保存当前的label
        currentIndex = targetIndex
    }
}
